package com.lzh.chatnote.user.dao;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.chatnote.chat.domain.enums.GroupMemberStatusEnum;
import com.lzh.chatnote.common.domian.vo.request.CursorPageBaseReq;
import com.lzh.chatnote.common.domian.vo.response.CursorPageBaseResp;
import com.lzh.chatnote.common.enums.CursorEnum;
import com.lzh.chatnote.common.utils.CursorUtils;
import com.lzh.chatnote.user.domain.entity.User;
import com.lzh.chatnote.user.mapper.UserMapper;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lzh
 * @since 2025-06-06
 */
@Service
public class UserDao extends ServiceImpl<UserMapper, User>{

    public User getUserByOpenId(String openid) {
        return lambdaQuery()
                .eq(User::getOpenId, openid)
                .one();
    }


    public List<User> getUserByIds(Set<Long> friendIds) {
        return lambdaQuery().eq(User::getId, friendIds)
                .list();
    }

    public List< User> batchLoadUser(List<Long> keys) {
        return Optional.ofNullable(lambdaQuery().in(User::getId, keys).list()).orElse(Collections.emptyList());
    }

    public CursorPageBaseResp<User> getCursorPage(List<Long> userIds, String cursorValue, CursorPageBaseReq cursorPageBaseReq, GroupMemberStatusEnum queryStatus) {
        return CursorUtils.queryCursorPage(this, cursorPageBaseReq,wrapper ->{
            wrapper.eq(User::getActiveStatus, queryStatus.getActiveStatus());
            wrapper.in(CollUtil.isNotEmpty(userIds),User::getId, userIds);
            wrapper.le(User::getLastOptTime, cursorValue);
        },User::getLastOptTime, CursorEnum.DESC);
    }
}
